import includes.http as http

global gateway
global debug

def init(init_debug, init_gateway):
	global debug
	global gateway
	debug=init_debug
	gateway=init_gateway
	
def i_get(command,valued=0):
	command=command.rstrip()
	instance=command.split(' ')[1]
	parameters=command.split(' ')
	parameter=''
	if valued :
		loop=parameters[3:-1]
		value=parameters[-1] 
	else:
		loop= parameters[3:]
		value='Unidentified'
	for param in loop:
		parameter+=  str(param) + '.'
	parameter=parameter[:-1]
	xml="<xmlrequest version=\"1.0.1\">\n<command inst=\"cfgmgr-0\"><key>begin_transaction</key><value>"+instance+"</value></command> <get inst=\""+instance+"\"><key>"+parameter+"</key><value>"+value+"</value></get><command inst=\"cfgmgr-0\"><key>end_transaction</key><value>"+instance+"</value></command> </xmlrequest>"
	if debug : print xml	
	xml=http.send_xml(xml, gateway)
	for k,v in xml.iteritems():
		if k in ["error",'None',''] and valued==0 :
			i_get(command,1)
		else:		
			pr_response(k,v)
	return xml

def i_set(command):
	command=command.rstrip()
	value=str(command.split(' ')[-1:][0])
	instance=str(command.split(' ')[1])
	parameters=command.split(' ')
	parameter=''
	for param in parameters[3:-1]:
		parameter+=  str(param) + '.'
	parameter=str(parameter[:-1])
	xml="<xmlrequest version=\"1.0.1\">\n<command inst=\"cfgmgr-0\"><key>begin_transaction</key><value>"+instance+"</value></command> <set inst=\""+instance+"\"><key>"+parameter+"</key><value>"+value+"</value></set><command inst=\"cfgmgr-0\"><key>commit</key><value>"+instance+"</value></command><command inst=\"cfgmgr-0\"><key>end_transaction</key><value>"+instance+"</value></command> <command inst=\"cfgmgr-0\"><key>save</key><value>undefined</value></command></xmlrequest>"
	if debug : print xml	
	xml=http.send_xml(xml, gateway)
	for k,v in xml.iteritems():
		pr_response(k,v)
	return xml

def i_query(command,valued=0):
	command=command.rstrip()
	instance=command.split(' ')[1]
	parameters=command.split(' ')
	parameter=''
	if valued :
		loop=parameters[3:-1]
		value=parameters[-1]
	else:
		loop= parameters[3:]
		value='Unidentified'
	for param in loop:
		parameter+=  str(param) + '.'
	parameter=parameter[:-1]
	xml="<xmlrequest version=\"1.0.1\">\n<query inst=\""+instance+"\"><key>"+parameter+"</key><value/></query></xmlrequest>"
	if debug : print xml	
	xml=http.send_xml(xml, gateway)
	for k,v in xml.iteritems():
		if k == "error" and valued==0 :
			i_get(command,1)
		else:
			pr_response(k,v)
	return xml

def i_command(command):
	instance=command.split(' ')[1]
	command=command.split(' ')[2]
	xml="<xmlrequest version=\"1.0.1\"><command inst=\""+instance+"\"><key>"+command+"</key><value>undefined</value></command> </xmlrequest>"	
	if debug : print xml
	xml=http.send_xml(xml, gateway)
	for k,v in xml.iteritems():
		print pr_response(k,v)
	return xml

def get_instances(manager):
	instances=http.send_xml("<xmlrequest version=\"1.0.1\"><query inst=\"devmgr-0\"><key>inst_list</key><value>"+manager+"</value></query></xmlrequest>",gateway)
	return instances.values()

def get_managers():
	xml="<xmlrequest version=\"1.0.1\"><query inst=\"devmgr-0\"><key>mngr_list</key><value/></query></xmlrequest>"
	managers=http.send_xml(xml, gateway)
	return managers.values()

def pr_response(k,v):
	errors={
	"1":"ERR_OK",
	"2":"ERR_UI_INVALID_SESSION",
	"3":"ERR_UIFX_INIT",
	"4":"ERR_NOT_ENOUGH_MEMORY",
	"5":"ERR_INVALID_XML_FORMAT",
	"6":"ERR_INVALID_COMMUNICATION_VERSION",
	"7":"ERR_INVALID_XML_CONTENT_NO_KEY",
	"8":"ERR_INVALID_XML_CONTENT_NO_VALUE",
	"9":"ERR_INVALID_XML_CONTENT_NO_INST",
	"10":" ERR_FW_INVALID_TAG",
	"11":" ERR_FW_INVALID_SIGN",
	"12":" ERR_FW_TOO_BIG",
	"13":" ERR_FW_INTERNAL",
	"14":" ERR_CFG_HEADER_CSUM",
	"15":" ERR_FILE_OPEN",
	"16":" ERR_FILE_READ",
	"17":" ERR_FILE_WRITE",
	"18":" ERR_FILE_STAT",
	"19":" ERR_MMAP_FAILED",
	"20":" ERR_UNDEFINED",
	"21":" ERR_MEM_ALLOC_FAILED",
	"22":" ERR_MALFORMED_STRING",
	"23":" ERR_BACKEND_OPERATION",
	"24":" ERR_NEED_DEFAULT_PASSWORD_CHANGE",
	}
	if str(k) == "error":
		print " " + str(k) + " : " + errors[str(v)]
	else:
		print " " + str(k) + " : " + str(v)
